﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine;
using PloobsEngine.SceneControl;
using PloobsEngine.Utils;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Light;
using Microsoft.Xna.Framework;

namespace Etapa1
{
    public class LightInterpolator : IScreenUpdateable
    {
        Vec3Interpolator vi = new Vec3Interpolator();
        Vector3 v1;
        Vector3 v2;        
        double duration;
        List<DeferredLight> lights = new List<DeferredLight>();
        bool onLoopReverse = false;

        public bool OnLoopReverse
        {
            get { return onLoopReverse; }
            set { onLoopReverse = value; }
        }

        public List<DeferredLight> Lights
        {
            get { return lights; }
            set { lights = value; }
        }

        public LightInterpolator(SceneScreen screen, Color c1, Color c2, double duration)
            : base(screen)
        {
            this.v1 = c1.ToVector3();
            this.v2 = c2.ToVector3();
            this.duration = duration;
            vi.Start(v1, v2, duration,true);
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            vi.Update(gameTime);
            foreach (var item in lights)
            {
                item.Color = new Color(vi.CurrentValue);
            }

            if (vi.IsActive == false && onLoopReverse == true)
            {
                RevertAndRestart();
            }
            
        }

        public bool isFinished
        {
            get
            {
                return vi.IsActive;
            }
        }

        public void Restart()
        {
            vi.Reset();
        }

        public void Restart(Color c1, Color c2, double duration)
        {
            this.v1 = c1.ToVector3();
            this.v2 = c2.ToVector3();
            this.duration = duration;
            vi.Start(v1, v2, duration, true);
        }

        public void RevertAndRestart()
        {
            Vector3 x = this.v1;
            this.v1 = this.v2;
            this.v2 = x;
            vi.Start(v1, v2, duration, true);
        }

        
    }
}
